package com.bt.mes.brm.provider.service;

import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_CANCEL;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_FILTER_DISTINCT;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISPAM_MDL_NOT_FOUNCT;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISPAM_NOT_FOUND_RECORDER;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISPAM_RECORDER_ALREADY_EXIST;
import static com.bt.mes.common.bean.def.GenericDef.E_XPWOTOOL_NOT_FOUND_RECORDER;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.bt.mes.brm.api.service.IXpbispamService;
import com.bt.mes.brm.provider.tx.Xpbispam.XpbispamI;
import com.bt.mes.brm.provider.tx.Xpbispam.XpbispamIA;
import com.bt.mes.brm.provider.tx.Xpbispam.XpbispamO;
import com.bt.mes.brm.provider.tx.Xpbispam.XpbispamOA;
import com.bt.mes.common.dao.IBisPamDao;
import com.bt.mes.common.util.MqOpe;
import com.bt.table.ppt.Bis_pam;
import com.bt.table.ppt.Bis_pamId;

@Service("xpbispamService")
public class XpbispamService implements IXpbispamService {
	private Logger logger = Logger.getLogger(XpbispamService.class);
	@Autowired
	private XpbispamI inTrx;

	@Autowired
	private XpbispamO outTrx;

	@Autowired
	private IBisPamDao bisPamDao;

	@Autowired
	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		logger.info("InTrx is " + strInMsg);
		Session session = bisPamDao.getCrSession();
		try {
			outTrx.setTrx_id("XPBISPAM");
			outTrx.setType_id("O");
			inTrx = (XpbispamI) mqOpe.inStr2Obj(strInMsg, inTrx);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition();
				break;
			case ACTION_FLG_ADD:
				addFnc();
				break;
			case ACTION_FLG_UPDATE:
				updateFnc();
				break;
			case ACTION_FLG_DELETE:
				deleteFnc();
				break;
			case ACTION_FLG_FILTER_DISTINCT:
				listDistinctFnc();
				break;
			case ACTION_FLG_CANCEL:
				deleteByConditionFnc();
				break;
			case 'K':
				findRealOneParam();
				break;// 查询实际使用的param
			default:
				errActionFnc();
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}

		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			// TODO:throw dbRollBackException
		} finally {
			logger.info(mqOpe.obj2Str(outTrx));
		}
		return mqOpe.obj2Str(outTrx);
	}

	private void findRealOneParam() {

		/***
		 * 支持
		 * 
		 * by bay_id,wo_id_fk,ope_id_fk 查询
		 * 
		 **/

		List<XpbispamOA> oaryList = new ArrayList<XpbispamOA>();

		XpbispamIA iary = inTrx.getIary().get(0);
		String tool_id_fk = iary.getTool_id_fk();
		// String mdl_typ_fk=iary.getMdl_typ_fk();
		String mdl_id_fk = iary.getMdl_id_fk();
		String path_id_fk = iary.getPath_id_fk();
		String path_ver_fk = iary.getPath_ver_fk();
		String ope_no_fk = iary.getOpe_no_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		String ope_ver_fk = iary.getOpe_ver_fk();

		StringBuffer hql = new StringBuffer();
		hql.append("FROM Bis_pam where 1=1 ");
		hql.append(" AND tool_id_fk ='").append(tool_id_fk).append("' ");
		hql.append(" AND mdl_id_fk in('*','").append(mdl_id_fk).append("') ");
		hql.append(" AND path_id_fk in('*','").append(path_id_fk).append("') ");
		hql.append(" AND path_ver_fk in('*','").append(path_ver_fk).append("') ");
		hql.append(" AND ope_no_fk in('*','").append(ope_no_fk).append("') ");
		hql.append(" AND ope_id_fk in('*','").append(ope_id_fk).append("') ");
		hql.append(" AND ope_ver_fk in('*','").append(ope_ver_fk).append("') ");
		hql.append(" order by mdl_id_fk desc,path_id_fk desc,path_ver_fk desc,");
		hql.append(" ope_no_fk desc,ope_id_fk desc,ope_ver_fk desc");
		List<Bis_pam> bisPamList = bisPamDao.find(hql.toString());

		oaryList.clear();
		XpbispamOA oary = new XpbispamOA();
		if (bisPamList.size() >= 1) {
			Bis_pam bis_pam = bisPamList.get(0);
			oary.setTool_id_fk(bis_pam.getId().getTool_id_fk());
			oary.setMdl_typ_fk(bis_pam.getId().getMdl_typ_fk());
			oary.setMdl_id_fk(bis_pam.getId().getMdl_id_fk());
			oary.setPath_id_fk(bis_pam.getId().getPath_id_fk());
			oary.setPath_ver_fk(bis_pam.getId().getPath_ver_fk());
			oary.setOpe_id_fk(bis_pam.getId().getOpe_id_fk());
			oary.setOpe_no_fk(bis_pam.getId().getOpe_no_fk());
			oary.setOpe_ver_fk(bis_pam.getId().getOpe_ver_fk());
			oary.setRecipe_id(bis_pam.getRecipe_id());
			oary.setActive_flg(bis_pam.getActive_flg());
			oary.setSkip_flg(bis_pam.getSkip_flg());
			oary.setMes_id(bis_pam.getMes_id());
			oary.setBox_set_code(bis_pam.getBox_set_code());
			oary.setGd_judge_flg(bis_pam.getGd_judge_flg());
			oary.setDef_judge_flg(bis_pam.getDef_judge_flg());
			oary.setSwh_cnt_add_flg(bis_pam.getSwh_cnt_add_flg());
			oary.setSwh_cnt_add_pos(bis_pam.getSwh_cnt_add_pos());
			oary.setSwh_max_cnt(bis_pam.getSwh_max_cnt());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");

	}

	public void addFnc() {

		XpbispamIA iary = inTrx.getIary().get(0);
		String tool_id_fk = iary.getTool_id_fk();
		String mdl_id_fk = iary.getMdl_id_fk();
		String mdl_typ_fk = (("*").equals(mdl_id_fk)) ? "*" : getMdlTypByMdlIDProc(mdl_id_fk);
		if (mdl_typ_fk == null) {
			outTrx.setRtn_code(E_XPBISPAM_MDL_NOT_FOUNCT + "");
			outTrx.setRtn_mesg("not found mdl_id[" + mdl_id_fk + "]");
			return;
		}
		String path_id_fk = iary.getPath_id_fk();
		String path_ver_fk = iary.getPath_ver_fk();
		String ope_no_fk = iary.getOpe_no_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		String ope_ver_fk = iary.getOpe_ver_fk();

		Bis_pamId id = new Bis_pamId();
		id.setTool_id_fk(tool_id_fk);
		id.setMdl_id_fk(mdl_id_fk);
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setPath_id_fk(path_id_fk);
		id.setPath_ver_fk(path_ver_fk);
		id.setOpe_no_fk(ope_no_fk);
		id.setOpe_id_fk(ope_id_fk);
		id.setOpe_ver_fk(ope_ver_fk);

		Bis_pam queryBisPam = bisPamDao.get(id);
		if (queryBisPam != null) {
			outTrx.setRtn_code(E_XPBISPAM_RECORDER_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("Record already Exist");
			return;
		}
		Bis_pam bis_pam = new Bis_pam();
		bis_pam.setId(id);
		bis_pam.setRecipe_id(iary.getRecipe_id());
		bis_pam.setActive_flg(iary.getActive_flg());
		bis_pam.setSkip_flg(iary.getSkip_flg());
		bis_pam.setMes_id(iary.getMes_id());
		bis_pam.setBox_set_code(iary.getBox_set_code());
		bis_pam.setGd_judge_flg(iary.getGd_judge_flg());
		bis_pam.setDef_judge_flg(iary.getDef_judge_flg());
		bis_pam.setSwh_cnt_add_flg(iary.getSwh_cnt_add_flg());
		bis_pam.setSwh_cnt_add_pos(iary.getSwh_cnt_add_pos());
		bis_pam.setSwh_max_cnt(iary.getSwh_max_cnt());
		bisPamDao.save(bis_pam);

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");

	}

	public void updateFnc() {

		XpbispamO xpbispamO = new XpbispamO();
		xpbispamO.setTrx_id("XPBISPAM");
		xpbispamO.setType_id("O");

		XpbispamIA iary = inTrx.getIary().get(0);
		String tool_id_fk = iary.getTool_id_fk();
		String mdl_id_fk = iary.getMdl_id_fk();
		String mdl_typ_fk = (("*").equals(mdl_id_fk)) ? "*" : getMdlTypByMdlIDProc(mdl_id_fk);
		if (mdl_typ_fk == null) {
			xpbispamO.setRtn_code(E_XPBISPAM_MDL_NOT_FOUNCT + "");
			xpbispamO.setRtn_mesg("not found mdl_id[" + mdl_id_fk + "]");
			return;
		}
		String path_id_fk = iary.getPath_id_fk();
		String path_ver_fk = iary.getPath_ver_fk();
		String ope_no_fk = iary.getOpe_no_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		String ope_ver_fk = iary.getOpe_ver_fk();

		Bis_pamId id = new Bis_pamId();
		id.setTool_id_fk(tool_id_fk);
		id.setMdl_id_fk(mdl_id_fk);
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setPath_id_fk(path_id_fk);
		id.setPath_ver_fk(path_ver_fk);
		id.setOpe_no_fk(ope_no_fk);
		id.setOpe_id_fk(ope_id_fk);
		id.setOpe_ver_fk(ope_ver_fk);

		Bis_pam bis_pam = bisPamDao.get(id);
		if (bis_pam == null) {
			xpbispamO.setRtn_code(E_XPBISPAM_NOT_FOUND_RECORDER + "");
			xpbispamO.setRtn_mesg("not found record");
			return;
		}
		// 获取主键
		bis_pam.setId(id);
		if (iary.getRecipe_id() != null) {
			bis_pam.setRecipe_id(iary.getRecipe_id());
		}
		if (iary.getActive_flg() != null) {
			bis_pam.setActive_flg(iary.getActive_flg());
		}
		if (iary.getSkip_flg() != null) {
			bis_pam.setSkip_flg(iary.getSkip_flg());
		}
		if (iary.getMes_id() != null) {
			bis_pam.setMes_id(iary.getMes_id());
		}
		if (iary.getBox_set_code() != null) {
			bis_pam.setBox_set_code(iary.getBox_set_code());
		}
		if (iary.getGd_judge_flg() != null) {
			bis_pam.setGd_judge_flg(iary.getGd_judge_flg());
		}
		if (iary.getDef_judge_flg() != null) {
			bis_pam.setDef_judge_flg(iary.getDef_judge_flg());
		}
		if (iary.getSwh_cnt_add_flg() != null) {
			bis_pam.setSwh_cnt_add_flg(iary.getSwh_cnt_add_flg());
		}
		if (iary.getSwh_cnt_add_pos() != null) {
			bis_pam.setSwh_cnt_add_pos(iary.getSwh_cnt_add_pos());
		}
		if (iary.getSwh_max_cnt() != null) {
			bis_pam.setSwh_max_cnt(iary.getSwh_max_cnt());
		}
		bisPamDao.update(bis_pam);

		xpbispamO.setRtn_code(RETURN_CODE_OK);
		xpbispamO.setRtn_mesg("success");
		return;

	}

	public void deleteFnc() {
		outTrx.setTrx_id("XPBISPAM");
		outTrx.setType_id("O");
		XpbispamIA iary = inTrx.getIary().get(0);

		String tool_id_fk = iary.getTool_id_fk();
		String mdl_id_fk = iary.getMdl_id_fk();
		String mdl_typ_fk = (("*").equals(mdl_id_fk)) ? "*" : getMdlTypByMdlIDProc(mdl_id_fk);
		if (mdl_typ_fk == null) {
			outTrx.setRtn_code(E_XPBISPAM_MDL_NOT_FOUNCT + "");
			outTrx.setRtn_mesg("not found mdl_id[" + mdl_id_fk + "]");
			return;
		}
		String path_id_fk = iary.getPath_id_fk();
		String path_ver_fk = iary.getPath_ver_fk();
		String ope_no_fk = iary.getOpe_no_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		String ope_ver_fk = iary.getOpe_ver_fk();

		Bis_pamId id = new Bis_pamId();
		id.setTool_id_fk(tool_id_fk);
		id.setMdl_id_fk(mdl_id_fk);
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setPath_id_fk(path_id_fk);
		id.setPath_ver_fk(path_ver_fk);
		id.setOpe_no_fk(ope_no_fk);
		id.setOpe_id_fk(ope_id_fk);
		id.setOpe_ver_fk(ope_ver_fk);

		Bis_pam bis_pam = bisPamDao.get(id);

		if (bis_pam == null) {
			outTrx.setRtn_code(E_XPWOTOOL_NOT_FOUND_RECORDER + "");
			outTrx.setRtn_mesg("not found record");
			return;
		}

		bisPamDao.delete(bis_pam);

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return;
	}

	public void queryByCondition() {

		/***
		 * 支持
		 * 
		 * by bay_id,wo_id_fk,ope_id_fk 查询
		 * 
		 **/

		XpbispamO outTrx = new XpbispamO();
		outTrx.setTrx_id("XPBISPAM");
		outTrx.setType_id("O");
		List<XpbispamOA> oaryList = new ArrayList<XpbispamOA>();

		XpbispamIA iary = inTrx.getIary().get(0);
		String tool_id_fk = iary.getTool_id_fk();
		String mdl_typ_fk = iary.getMdl_typ_fk();
		String mdl_id_fk = iary.getMdl_id_fk();
		String path_id_fk = iary.getPath_id_fk();
		String path_ver_fk = iary.getPath_ver_fk();
		String ope_no_fk = iary.getOpe_no_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		String ope_ver_fk = iary.getOpe_ver_fk();

		String hql = "FROM Bis_pam where 1=1 ";
		if (tool_id_fk != null) {
			hql = hql + " and tool_id_fk='" + tool_id_fk + "'";

		}
		if (mdl_typ_fk != null) {
			hql = hql + " and mdl_typ_fk ='" + mdl_typ_fk + "'";

		}
		if (mdl_id_fk != null) {
			hql = hql + " and mdl_id_fk ='" + mdl_id_fk + "'";

		}
		if (path_id_fk != null) {
			hql = hql + " and path_id_fk ='" + path_id_fk + "'";

		}
		if (path_ver_fk != null) {
			hql = hql + " and path_ver_fk ='" + path_ver_fk + "'";

		}
		if (ope_no_fk != null) {
			hql = hql + " and ope_no_fk ='" + ope_no_fk + "'";

		}
		if (ope_id_fk != null) {
			hql = hql + " and ope_id_fk ='" + ope_id_fk + "'";

		}
		if (ope_ver_fk != null) {
			hql = hql + " and ope_ver_fk ='" + ope_ver_fk + "'";

		}
		hql = hql + " order by mdl_typ_fk,mdl_id_fk,path_id_fk,path_ver_fk,"
				+ "ope_no_fk,ope_id_fk,ope_ver_fk,tool_id_fk";
		List<Bis_pam> bisPamList = bisPamDao.find(hql);

		for (Bis_pam bis_pam : bisPamList) {
			XpbispamOA oary = new XpbispamOA();
			oary.setTool_id_fk(bis_pam.getId().getTool_id_fk());
			oary.setMdl_typ_fk(bis_pam.getId().getMdl_typ_fk());
			oary.setMdl_id_fk(bis_pam.getId().getMdl_id_fk());
			oary.setPath_id_fk(bis_pam.getId().getPath_id_fk());
			oary.setPath_ver_fk(bis_pam.getId().getPath_ver_fk());
			oary.setOpe_id_fk(bis_pam.getId().getOpe_id_fk());
			oary.setOpe_no_fk(bis_pam.getId().getOpe_no_fk());
			oary.setOpe_ver_fk(bis_pam.getId().getOpe_ver_fk());
			oary.setRecipe_id(bis_pam.getRecipe_id());
			oary.setActive_flg(bis_pam.getActive_flg());
			oary.setSkip_flg(bis_pam.getSkip_flg());
			oary.setMes_id(bis_pam.getMes_id());
			oary.setBox_set_code(bis_pam.getBox_set_code());
			oary.setGd_judge_flg(bis_pam.getGd_judge_flg());
			oary.setDef_judge_flg(bis_pam.getDef_judge_flg());
			oary.setSwh_cnt_add_flg(bis_pam.getSwh_cnt_add_flg());
			oary.setSwh_cnt_add_pos(bis_pam.getSwh_cnt_add_pos());
			oary.setSwh_max_cnt(bis_pam.getSwh_max_cnt());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");

	}

	public void listDistinctFnc() {
		/***
		 * 支持
		 * 
		 * by bay_id,wo_id_fk,ope_id_fk 查询
		 * 
		 **/

		List<XpbispamOA> oaryList = new ArrayList<XpbispamOA>();

		XpbispamIA iary = inTrx.getIary().get(0);
		String tool_id_fk = iary.getTool_id_fk();
		String mdl_typ_fk = iary.getMdl_typ_fk();
		String mdl_id_fk = iary.getMdl_id_fk();
		String path_id_fk = iary.getPath_id_fk();
		String path_ver_fk = iary.getPath_ver_fk();
		String ope_no_fk = iary.getOpe_no_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		String ope_ver_fk = iary.getOpe_ver_fk();

		String hql = "FROM Bis_pam where 1=1 ";
		if (tool_id_fk != null) {
			if (tool_id_fk.indexOf('*') == -1) {
				hql = hql + " and tool_id_fk='" + tool_id_fk + "'";
			} else {
				hql = hql + " and tool_id_fk like '" + tool_id_fk.replace('*', '%') + "'";
			}
			// hql = hql + " tool_id_fk ='"+ tool_id_fk +"'" ;

		}
		if (mdl_typ_fk != null) {
			if (mdl_typ_fk.indexOf('*') == -1) {
				hql = hql + " and mdl_typ_fk='" + mdl_typ_fk + "'";
			} else {
				hql = hql + " and mdl_typ_fk like '" + mdl_typ_fk.replace('*', '%') + "'";
			}
			// hql = hql + " mdl_typ_fk ='"+ mdl_typ_fk +"'" ;

		}
		if (mdl_id_fk != null) {
			if (mdl_id_fk.indexOf('*') == -1) {
				hql = hql + " and mdl_id_fk='" + mdl_id_fk + "'";
			} else {
				hql = hql + " and mdl_id_fk like '" + mdl_id_fk.replace('*', '%') + "'";
			}
			// hql = hql + " mdl_id_fk ='"+ mdl_id_fk +"'" ;

		}
		if (path_id_fk != null) {
			if (path_id_fk.indexOf('*') == -1) {
				hql = hql + " and path_id_fk='" + path_id_fk + "'";
			} else {
				hql = hql + " and path_id_fk like '" + path_id_fk.replace('*', '%') + "'";
			}
			// hql = hql + " path_id_fk ='"+ path_id_fk +"'" ;

		}
		if (path_ver_fk != null) {
			if (path_ver_fk.indexOf('*') == -1) {
				hql = hql + " and path_ver_fk='" + path_ver_fk + "'";
			} else {
				hql = hql + " and path_ver_fk like '" + path_ver_fk.replace('*', '%') + "'";
			}
			// hql = hql + " path_ver_fk ='"+ path_ver_fk +"'" ;

		}
		if (ope_no_fk != null) {
			if (ope_no_fk.indexOf('*') == -1) {
				hql = hql + " and ope_no_fk='" + ope_no_fk + "'";
			} else {
				hql = hql + " and ope_no_fk like '" + ope_no_fk.replace('*', '%') + "'";
			}
			// hql = hql + " ope_no_fk ='"+ ope_no_fk +"'" ;

		}
		if (ope_id_fk != null) {
			if (ope_id_fk.indexOf('*') == -1) {
				hql = hql + " and ope_id_fk='" + ope_id_fk + "'";
			} else {
				hql = hql + " and ope_id_fk like '" + ope_id_fk.replace('*', '%') + "'";
			}
			// hql = hql + " ope_id_fk ='"+ ope_id_fk +"'" ;

		}
		if (ope_ver_fk != null) {
			if (ope_ver_fk.indexOf('*') == -1) {
				hql = hql + " and ope_ver_fk='" + ope_ver_fk + "'";
			} else {
				hql = hql + " and ope_ver_fk like '" + ope_ver_fk.replace('*', '%') + "'";
			}
			// hql = hql + " ope_ver_fk ='"+ ope_ver_fk +"'" ;

		}
		hql = hql + " order by mdl_typ_fk,mdl_id_fk,path_id_fk,path_ver_fk,"
				+ "ope_no_fk,ope_id_fk,ope_ver_fk,tool_id_fk";
		List<Bis_pam> bisPamList = bisPamDao.find(hql);
		String mdl_typ_fk2 = "";
		String mdl_id_fk2 = "";
		String path_id_fk2 = "";
		String path_ver_fk2 = "";
		String ope_id_fk2 = "";
		String ope_no_fk2 = "";
		String ope_ver_fk2 = "";

		for (Bis_pam bis_pam : bisPamList) {
			XpbispamOA oary = new XpbispamOA();
			if (bis_pam.getId().getMdl_typ_fk().equals(mdl_typ_fk2) && bis_pam.getId().getMdl_id_fk().equals(mdl_id_fk2)
					&& bis_pam.getId().getPath_id_fk().equals(path_id_fk2)
					&& bis_pam.getId().getPath_ver_fk().equals(path_ver_fk2)
					&& bis_pam.getId().getOpe_id_fk().equals(ope_id_fk2)
					&& bis_pam.getId().getOpe_no_fk().equals(ope_no_fk2)
					&& bis_pam.getId().getOpe_ver_fk().equals(ope_ver_fk2)) {
				continue;
			}
			oary.setTool_id_fk(bis_pam.getId().getTool_id_fk());
			oary.setMdl_typ_fk(bis_pam.getId().getMdl_typ_fk());
			oary.setMdl_id_fk(bis_pam.getId().getMdl_id_fk());
			oary.setPath_id_fk(bis_pam.getId().getPath_id_fk());
			oary.setPath_ver_fk(bis_pam.getId().getPath_ver_fk());
			oary.setOpe_id_fk(bis_pam.getId().getOpe_id_fk());
			oary.setOpe_no_fk(bis_pam.getId().getOpe_no_fk());
			oary.setOpe_ver_fk(bis_pam.getId().getOpe_ver_fk());
			oary.setRecipe_id(bis_pam.getRecipe_id());
			oary.setActive_flg(bis_pam.getActive_flg());
			oary.setSkip_flg(bis_pam.getSkip_flg());
			oary.setMes_id(bis_pam.getMes_id());
			oary.setBox_set_code(bis_pam.getBox_set_code());
			oary.setGd_judge_flg(bis_pam.getGd_judge_flg());
			oary.setDef_judge_flg(bis_pam.getDef_judge_flg());
			oary.setSwh_cnt_add_flg(bis_pam.getSwh_cnt_add_flg());
			oary.setSwh_cnt_add_pos(bis_pam.getSwh_cnt_add_pos());
			oary.setSwh_max_cnt(bis_pam.getSwh_max_cnt());
			oaryList.add(oary);
			mdl_typ_fk2 = bis_pam.getId().getMdl_typ_fk();
			mdl_id_fk2 = bis_pam.getId().getMdl_id_fk();
			path_id_fk2 = bis_pam.getId().getPath_id_fk();
			path_ver_fk2 = bis_pam.getId().getPath_ver_fk();
			ope_id_fk2 = bis_pam.getId().getOpe_id_fk();
			ope_no_fk2 = bis_pam.getId().getOpe_no_fk();
			ope_ver_fk2 = bis_pam.getId().getOpe_ver_fk();
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}

	public void deleteByConditionFnc() {
		XpbispamIA iary = inTrx.getIary().get(0);
		String hql = "FROM Bis_pam where  mdl_id_fk='" + iary.getMdl_id_fk() + "'";
		hql = hql + " and mdl_typ_fk='" + iary.getMdl_typ_fk() + "'";
		hql = hql + " and ope_id_fk='" + iary.getOpe_id_fk() + "'";
		hql = hql + " and ope_no_fk='" + iary.getOpe_no_fk() + "'";
		hql = hql + " and ope_ver_fk='" + iary.getOpe_ver_fk() + "'";
		hql = hql + " and path_id_fk='" + iary.getPath_id_fk() + "'";
		hql = hql + " and path_ver_fk='" + iary.getPath_ver_fk() + "'";

		List<Bis_pam> bisPamList = bisPamDao.find(hql);
		for (Bis_pam bis_pam : bisPamList) {
			bisPamDao.delete(bis_pam);
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return;
	}

	public void errActionFnc() {
		outTrx.setTrx_id("XPBISPAM");
		outTrx.setType_id("O");
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}

	private String getMdlTypByMdlIDProc(String mdl_id) {
		String sql = "SELECT MDL_TYP FROM BIS_MDL_DEF WHERE MDL_ID='" + mdl_id + "'";
		List mdlTypeList = bisPamDao.findBySQL(sql);
		if (mdlTypeList.size() == 0) {
			return null;
		}
		return mdlTypeList.get(0).toString();
	}
}
